<?php

use yii\db\Migration;

class m180921_124921_create_agent_publish extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%agent_publish}}', [
            'id' => $this->primaryKey(),
            'agent_id' => $this->integer()->notNull()->comment('代理商编号'),
            'name' => $this->string(32)->notNull()->comment('名称'),
            'type' => $this->integer()->notNull()->comment('类型'),
            'value' => $this->text()->notNull()->comment('内容'),
            'sort' => $this->integer()->notNull()->defaultValue(0)->comment('排序'),
        ]);
        $this->createIndex('fk_agent_publish_agent1_idx', '{{%agent_publish}}', ['agent_id']);
        try {
            $this->addForeignKey('fk_agent_publish_agent1', '{{%agent_publish}}', ['agent_id'], '{{%agent}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['agent_publish_type', 1, '单行文本'],
            ['agent_publish_type', 2, '多行文本'],
            ['agent_publish_type', 3, '富文本'],
            ['agent_publish_type', 4, '文件'],
            ['agent_publish_type', 5, '图像'],
        ]);
    }

    public function safeDown()
    {
        $this->delete('{{%key_map}}', ['t' => 'agent_publish_type']);
        $this->dropTable('{{%agent_publish}}');
    }
}
